<?php
/**
 * form控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Ads extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel("ads_banner_model");

		$this->loadClass('Mcache');

		$this->tpl->template_dir = SYS_PATH  . 'template/web/formwork/';
	}

	/**
	 * 获取广告
	 * @return max
	 */
	function getAds(){
		
		$key    = intval($this->in_var['key']);
		
		$banner = $this->ads_banner_model->getBannerByZone($key);
		
		$tpl_name = $banner['b_mtype']['mt_formwork_name'];
		
		if($this->tpl->isCached($tpl_name, $key))
		{
			$this->tpl->displayCache($tpl_name,$key);
			exit;
		}
		else
		{
			$this->tpl->assign("banner",$banner);
			$this->tpl->displayCache($tpl_name,$key);
		}
		
	}

	/**
	 * 预览
	 * @return max
	 */
	function preview(){

		$key    = intval($this->in_var['key']);
		
		$banner = $this->ads_banner_model->getAdsBannerById($key);
		
		$tpl_name = $banner['b_mtype']['mt_formwork_name'];
		
		$this->tpl->assign("banner",$banner);
		$this->tpl->display($tpl_name);

	}
	
	/**
	 * 图片外链跳转统计
	 * @return max
	 */
	function redirect(){

		$redirect  = $this->in_var["seq"];
		$banner_id = intval($this->in_var["loc"]);
		$file_sort = intval($this->in_var["sid"]);
		/*
		 $banner = $this->Mcache->getCache("ads_hit",$banner_id);

		 if($banner){
			$banner[$file_sort] +=1 ;
			if($banner[$file_sort]>50)
			{

			}
			}
			else
			{
			$this->loadModel("ads_banner_model");
			$banner = $this->ads_banner_model->getAdsBannerById($banner_id);
			}

			$this->Mcache->setCache("ads_hit",$banner_id,$banner);
			$this->Mcache->setCache();
			*/
		goto($redirect,'header','document',false);
	}
	
	/**
	 * 判断当前广告位是否有广告，如果有则返回对应js无则返回空
	 * @return unknown_type
	 */
	function proxy(){
		
		$zone_id = intval($this->in_var['ads']);
		$b_id    = intval($this->in_var['banner']);
		
		$banner = $this->Mcache->getCache('banner',$b_id);
		
		if(empty($banner))
		{
			$banner = $this->ads_banner_model->getBannerByZone($zone_id);
			$this->Mcache->setCache('banner',$b_id,$banner);
		}
		
		if(empty($banner['b_status'])) exit;
		header("Content-Type: text/javascript");
		exit(file_get_contents("http://res.61.com/js/ads/info.js"));
	}

}